#!/bin/bash
# 请在dify/docker目录下执行:bash auto.sh
HOST="127.0.0.1"
EMAIL="123456@qq.com"
PASSWORD="1234"
MODEL="qwen-max"
ENDPOINT_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
API_KEY="sk-"

# 启动服务
docker compose up -d

# 初始化
for ((i=1; i<=10; i++))
do
  echo "系统初始化中..."
  setup_response=$(curl -s "http://$HOST/console/api/setup" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $access_token")
  if echo "$setup_response" | grep -q '"step": "finished"'; then
    echo "系统初始化成功"
    break
  fi
  if echo "$setup_response" | grep -q '"step": "not_started"'; then
    echo "系统初始化成功"
    init_response=$(curl -s "http://$HOST/console/api/init" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer $access_token")
    sleep 2
    if echo "$init_response" | grep -q '"status": "finished"'; then
      echo "管理员账号注册..."
      reg_response=$(curl -s POST "http://$HOST/console/api/setup" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer $access_token" \
        -d '{"email":"'$EMAIL'","name":"admin","password":"'$PASSWORD'"}')
        echo "管理员账号注册成功"
        break
    fi
  fi
  sleep 6
done
# 自动登录
login_response=$(curl -s POST "http://$HOST/console/api/login" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "'$EMAIL'",
    "password": "'$PASSWORD'",
    "remember_me": true,
    "language": "zh-Hans"
  }')

if ! echo "$login_response" | grep -q '"result": "success"'; then
    echo "登录失败"
    exit 1
fi
echo "登录成功"
access_token=$(echo "$login_response" | grep -o '"access_token": *"[^"]*"' | cut -d'"' -f4)

# 配置模型供应商
model_response=$(curl -s -X POST "http://$HOST/console/api/workspaces/current/model-providers/openai_api_compatible/models" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $access_token" \
  -d '{                  
      "model": "'"$MODEL"'", 
      "model_type": "llm", 
      "credentials": {
          "mode": "chat",
          "context_size": "4096",
          "max_tokens_to_sample": "4096",
          "function_calling_type": "no_call",
          "stream_function_calling": "not_supported",
          "vision_support": "no_support",
          "stream_mode_delimiter": "\\n\\n",
          "voices": "alloy",
          "api_key": "'"$API_KEY"'",
          "endpoint_url": "'"$ENDPOINT_URL"'"
      },
      "load_balancing": {
          "enabled": false,
          "configs": []
      }
    }')

if ! echo "$model_response" | grep -q '"result": "success"'; then
    echo "模型供应商配置失败"
    exit 1
fi
echo "模型供应商配置成功"

# 配置默认模型供应商
default_model_response=$(curl -s -X POST "http://$HOST/console/api/workspaces/current/default-model" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $access_token" \
  -d '{
        "model_settings": [{
                "model_type": "llm",
                "provider": "openai_api_compatible",
                "model": "'"$MODEL"'"
            }, {
                "model_type": "text-embedding"
            }, {
                "model_type": "rerank"
            }, {
                "model_type": "speech2text"
            }, {
                "model_type": "tts"
            }
        ]
      }')

if ! echo "$default_model_response" | grep -q '"result": "success"'; then
    echo "默认模型供应商配置失败"
    exit 1
fi

# 创建聊天助手
apps_response=$(curl -s -X POST "http://$HOST/console/api/apps" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $access_token" \
  -d '{
      "name": "聊天助手",
      "icon_type": "emoji",
      "icon": "🤖",
      "icon_background": "#FFEAD5",
      "mode": "chat",
      "description": ""
  }')

chat_id=$(echo "$apps_response" | grep -o '"id": *"[^"]*"' | cut -d'"' -f4)
echo "聊天助手创建成功"

# 聊天助手运行地址
chat_response=$(curl -s "http://$HOST/console/api/apps/$chat_id" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $access_token")

app_base_url=$(echo "$chat_response" | grep -o '"app_base_url": *"[^"]*"' | cut -d'"' -f4)
code=$(echo "$chat_response" | grep -o '"code": *"[^"]*"' | cut -d'"' -f4)

echo "Chat URL: $app_base_url/chat/$code"